<?php
require_once(dirname(__FILE__).'/inc/config.inc.php');



// 工单列表
$sql = "SELECT * FROM `#@__order` WHERE  delstate='' AND qiandao='true' AND driving_distance='0'  order by id desc limit 50   ";
$dosql->Execute($sql,8);
while($row = $dosql->GetArray(8))
{
   // 获取 接单位置
	$lat1="";
	$lng1="";
	$lat2="";
	$lng2="";
	$order_id=$row['id'];
	$row_j = $dosql->GetOne("SELECT * FROM `#@__order_log` WHERE `orderid`='$order_id'  and jiedan='true' ");
	if(is_array($row_j)  && isset($row_j['id']) ){
		if($row_j['latitude']!="" ){
			$lat1=$row_j['latitude'];
			$lng1=$row_j['longitude'];
		}

	}
	$row_q = $dosql->GetOne("SELECT * FROM `#@__order_log` WHERE `orderid`='$order_id'  and qiandao='true' ");
	if(is_array($row_q)  && isset($row_q['id']) ){
		if($row_q['latitude']!="" ){
			$lat2=$row_q['latitude'];
			$lng2=$row_q['longitude'];
		}

	}

	echo $lat1.",".$lng1."    ".$lat2.",".$lng2."<br>";
	$aa= driving_distance($lng1, $lat1, $lng2, $lat2);
	$dosql->ExecNoneQuery("update  `#@__order` set driving_distance='$aa' where id='$order_id' ");

  // 获取  签到位置

}




// 计算导航距离
#lon为经度，lat为纬度，一定不要弄错了哦
function driving_distance($lng1, $lat1, $lng2, $lat2){
	$distance="";
	$url='https://apis.map.qq.com/ws/direction/v1/driving/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&policy=LEAST_TIME&output=json&key=DRXBZ-Q3CHG-6KOQN-IJY6G-LDCS5-W4BZZ';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	//curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
	//curl_setopt($ch, CURLOPT_POST, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	$html = curl_exec($ch);
	$json_arr=json_decode($html,true);
	if($json_arr['status']==0){
		$distance=$json_arr['result']['routes'][0]['distance'];
	}
	return $distance;

}



?>